-- 无声的歌者
local wushendegezhe = fk.CreateSkill {
    name = "mcdh_wushendegezhe",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

wushendegezhe:addEffect('active',{
    target_num = 0,
    card_num = 1,
    card_filter = function(self, to_select, selected)
        return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeTrick
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        room:recastCard(effect.cards,player, self.name)
        local choices = { "wushendegezhe1", "wushendegezhe2","wushendegezhe3" }
        local choice = room:askForChoice(player, choices, self.name, nil, nil,choices)
        local mark = player:getTableMark("wushendegezhe_use-turn")
        table.insertIfNeed(mark,choice)
        room:setPlayerMark(player,"wushendegezhe_use-turn",mark)
        if choice == "wushendegezhe1" then
            room:setPlayerMark(player,"@@wushendegezhe1-turn",1)
        elseif choice == "wushendegezhe2" then
            room:setPlayerMark(player,"@@wushendegezhe2-turn",1)
        elseif choice == "wushendegezhe3" then
            room:addPlayerMark(player,"@wushendegezhe3-turn",1)
        end
    end
})

local refresh = {
    can_refresh = function(self, event, target, player, data)
        return ((event == fk.CardUsing or fk.AfterCardTargetDeclared) and target == player and data.card.trueName == "slash") or (event == fk.TargetSpecified  and data.extra_data and data.extra_data.wushendegezhe1)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        if event == fk.CardUsing then
            if player:getMark("@@wushendegezhe1-turn") ~= 0 then
                data.extra_data = data.extra_data or {}
                data.extra_data.wushendegezhe1 = true
                room:setPlayerMark(player,"@@wushendegezhe1-turn",0)
            end
            if player:getMark("@@wushendegezhe2-turn") ~= 0 then
                data.extraUse = true
                room:setPlayerMark(player,"@@wushendegezhe2-turn",0)
            end
        elseif event == fk.TargetSpecified then
            data.to:addQinggangTag(data)
        elseif event == fk.AfterCardTargetDeclared then
            if player:getMark("@wushendegezhe3-turn") ~= 0 then
                data.extra_data = data.extra_data or {}
                data.extra_data.wushendegezhe3 = player:getMark("@wushendegezhe3-turn")
                room:setPlayerMark(player,"@wushendegezhe3-turn",0)
            end
        end
    end
}

wushendegezhe:addEffect(fk.CardUsing,refresh)

wushendegezhe:addEffect(fk.TargetSpecified,refresh)

wushendegezhe:addEffect(fk.AfterCardTargetDeclared,refresh)

wushendegezhe:addEffect(fk.AfterCardTargetDeclared,{
    can_trigger = function(self, event, target, player, data)
        return target == player and data.extra_data and data.extra_data.wushendegezhe3 and #player.room:getUseExtraTargets(data) > 0
    end,
    on_cost = function(self, event, target, player, data)
        local n = data.extra_data.wushendegezhe3
        local tos = player.room:askForChoosePlayers(player, data:getExtraTargets(), 1, n,
                    "#wushendegezhe-choose:::"..data.card:toLogString()..":"..tostring(n), self.name, true)
        if #tos > 0 then
            self.cost_data = tos
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
            table.insertTable(data.tos, table.map(self.cost_data, function (p)
                return {p}
            end))
    end
})

wushendegezhe:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
            return player:hasSkill(self) and #player:getTableMark("wushendegezhe_use-turn") == 3 and target == player and target.phase == Player.Finish
    end,
    on_cost = function(self, event, target, player, data)
            local room = player.room
            local cards =  room:getCardsFromPileByRule(".|.|.|.|.|equip",1,"discardPile")
            if #cards == 1 then
                self.cost_data = cards[1]
                return true
            end
    end,
    on_use = function(self, event, target, player, data)
            local room = player.room
            room:obtainCard(player, self.cost_data, true, fk.ReasonJustMove,player.id, self.name)
            local card  = Fk:getCardById(self.cost_data)
            if not player.dead and not player:isProhibited(player, card) and not player:prohibitUse(card) and
                    table.contains(player:getCardIds("h"), self.cost_data) and
                    room:askForSkillInvoke(player, self.name, data, "#wushendegezhe-use:::"..card:toLogString()) then
                room:useCard({
                    from = player,
                    tos = {player.id},
                    card = card,
                })
            end
    end
})

wushendegezhe:addEffect('targetmod',{
    bypass_distances = function(self, player, skill, card)
        return player:getMark("@@wushendegezhe1-turn") ~= 0 and card and card.trueName == "slash"
    end,
    bypass_times = function(self, player, skill, scope)
        if player:getMark("@@wushendegezhe2-turn") ~= 0 and skill.trueName == "slash_skill"
                and scope == Player.HistoryPhase then
            return true
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_BI_007"] = "锏",
    ["mcdh_wushendegezhe"] = "无声的歌者",
    [":mcdh_wushendegezhe"] = "出牌阶段，你可以重铸一张锦囊牌，然后选择一项，令本回合使用的下一张【杀】：1.无距离限制且无视防具；2.无次数限制；3.可以额外指定一名目标。结束阶段，若你本回合均选择过以上选项，则从弃牌堆中获得一张装备牌，且你可以使用此牌。",
    ["#wushendegezhe-use"] = "无声的歌者：你可以使用%arg",
    ["wushendegezhe1"] = "1.无距离限制且无视防具",
    ["wushendegezhe2"] = "2.无次数限制",
    ["wushendegezhe3"] = "3.可以额外指定一名目标",
    ["@@wushendegezhe1-turn"] = "歌者:距离",
    ["@@wushendegezhe2-turn"] = "歌者:次数",
    ["@wushendegezhe3-turn"] = "歌者:目标",
    ["#wushendegezhe-choose"]="无声的歌者:为此%arg额外指定至多%arg2个目标"
}

return wushendegezhe